﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace SSAT.Utils
{
    public static class ConnectionHelper
    {
        public static bool OpenConnection(SqlConnection connection)
        {
            return OpenConnection(connection, true, false);
        }

        public static bool OpenConnection(SqlConnection connection, bool showDialogOnError, bool throwException)
        {
            Verification.CheckArgumentForNull(connection, "connection");

            bool result = false;
            try
            {
                AppConsole.WriteLine(string.Format(Resources.TryingToOpenConnectionToTheServer, connection.DataSource));
                connection.Open();
                AppConsole.WriteLine(string.Format(Resources.ConnectionToServerWasEstablished, connection.DataSource));
                result = true;
            }
            catch (SqlException ex)
            {
                Logging.Handle(ex);
                if (connection != null)
                {
                    try
                    {
                        connection.Close();
                    }
                    catch (Exception innerEx)
                    {
                        Logging.Handle(innerEx);
                    }
                }
                if (showDialogOnError)
                {
                    UIHelper.ShowConnectionErrorDialog(ex);
                }
                if (throwException)
                {
                    throw;
                }
            }
            return result;
        }

        public static bool CloseConnection(SqlConnection connectionToClose)
        {
            return connectionToClose != null && CloseConnection(connectionToClose, false);
        }

        public static bool CloseConnection(SqlConnection connectionToClose, bool showDialog)
        {
            bool result = false;
            if (connectionToClose != null && connectionToClose.State != ConnectionState.Closed)
            {
                try
                {
                    AppConsole.WriteLine(string.Format(Resources.TryingToCloseConnectionToTheServer, connectionToClose.DataSource));
                    connectionToClose.Close();
                    result = true;
                    AppConsole.WriteLine(Resources.ConnectionClosed);
                }
                catch (SqlException ex)
                {
                    Logging.Handle(ex);
                    if (showDialog)
                    {
                        UIHelper.ShowConnectionErrorDialog(ex);
                    }
                }
            }
            return result;
        }

        public static bool CheckWhetherConnectionInSpecifiedState(SqlConnection Connection, ConnectionState State)
        {
            return Connection != null && Connection.State == State;
        }

        public static string GetServerName(SqlConnection connection)
        {
            Verification.CheckArgumentForNull(connection, "connection");
            return connection.DataSource;
        }
    }
}
